package com.dwx.guli.service.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.dwx.guli.service.edu.entity.Subject;
import com.dwx.guli.service.edu.entity.excel.ExcelSubjectData;
import com.dwx.guli.service.edu.entity.vo.SubjectVo;
import com.dwx.guli.service.edu.listener.ExcelSubjectDataListener;
import com.dwx.guli.service.edu.mapper.SubjectMapper;
import com.dwx.guli.service.edu.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author dwx
 * @since 2021-06-30
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Override
    public void batchImport(InputStream inputStream) {
        EasyExcel.read(inputStream, ExcelSubjectData.class, new ExcelSubjectDataListener(baseMapper))
                .excelType(ExcelTypeEnum.XLS)
                .sheet()
                .doRead();
    }

    @Override
    public List<SubjectVo> nestedList() {
        List<SubjectVo> subjectVos = baseMapper.selectSubjectVoList();
        List<SubjectVo> subjectVoList=new ArrayList<>();
        Map<String ,SubjectVo> map=new HashMap<>();
        for (SubjectVo subjectVo : subjectVos) {
            String id = subjectVo.getId();
            map.put(id,subjectVo);
        }
        for (SubjectVo subjectVo : subjectVos) {
            String parentId = subjectVo.getParentId();
            if("0".equals(parentId)){
                subjectVoList.add(subjectVo);
            }else {
                SubjectVo parent = map.get(parentId);
                parent.getChildren().add(subjectVo);
            }
        }
        return subjectVoList;
    }

}
